package com.zllkj.mm.module.main.fragments;

import android.content.Intent;
import android.preference.PreferenceManager;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.appcompat.widget.AppCompatImageView;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.orhanobut.hawk.Hawk;
import com.zllkj.mm.R;
import com.zllkj.mm.api.network.RetrofitHelper;
import com.zllkj.mm.api.network.RxHelper;
import com.zllkj.mm.api.network.subscriber.BaseRxSubscriber;
import com.zllkj.mm.api.network.subscriber.BaseString;
import com.zllkj.mm.base.BaseTitleFragment;
import com.zllkj.mm.common.ClientConfig;
import com.zllkj.mm.common.L;
import com.zllkj.mm.model.entity.UserSelfVo;
import com.zllkj.mm.module.freetime.MyFreeTimeFragment;
import com.zllkj.mm.module.home.fragments.EditInfoFragment;
import com.zllkj.mm.module.home.fragments.MyCollectionFragment;
import com.zllkj.mm.module.home.fragments.SettingFragment_mm;
import com.zllkj.mm.module.home.fragments.ShopApplyFragment;
import com.zllkj.mm.module.home.fragments.ShopIndex2Fragment;
import com.zllkj.mm.module.home.fragments.WalletFragment;

import com.zllkj.mm.module.shopping.fragment.OrderListFragment;
import com.zllkj.mm.module.shopping.fragment.ShoppingCardFragment;
import com.zllkj.mm.util.CircleImageView;
import com.zllkj.mm.util.EmptyUtils;
import com.zllkj.mm.util.GlideCacheUtil;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;


/**
 * 我的
 * Created by leon_ok.
 */

public class MineFragment extends BaseTitleFragment {

    public static UserSelfVo userInfo;

    @BindView(R.id.tvGPS)
    TextView tvGPS;
    @BindView(R.id.ivMsg)
    ImageView ivMsg;
    @BindView(R.id.llMsg)
    View llMsg;
    @BindView(R.id.ivTopBackGround)
    View ivTopBackGround;
    @BindView(R.id.ivhead)
    CircleImageView ivhead;
    @BindView(R.id.tvUserName)
    TextView tvUserName;
    @BindView(R.id.tvTag1)
    TextView tvTag1;
    @BindView(R.id.tvTag2)
    TextView tvTag2;
//    @BindView(R.id.MineRlInfo)
//    RelativeLayout MineRlInfo;
    @BindView(R.id.icon4)
    TextView icon4;
    @BindView(R.id.tvNum)
    TextView tvNum;
    @BindView(R.id.tvIntro)
    TextView tvIntro;
    @BindView(R.id.llMyNews)
    LinearLayout llMyNews;
    @BindView(R.id.myShopLayout)
    LinearLayout myShopLayout;
    @BindView(R.id.myOrderLayout)
    LinearLayout myOrderLayout;
    @BindView(R.id.myIdleLayout)
    LinearLayout myIdleLayout;
    @BindView(R.id.shoppingCartLayout)
    LinearLayout shoppingCartLayout;
    @BindView(R.id.walletLayout)
    LinearLayout walletLayout;
    @BindView(R.id.myFavoritesLayout)
    LinearLayout myFavoritesLayout;
    @BindView(R.id.setLayout)
    LinearLayout setLayout;
    @BindView(R.id.editInfoIv)
    ImageView editInfoIv;
    @BindView(R.id.llImg)
    RecyclerView myPicListRV;

    public static MineFragment newInstance() {
        return new MineFragment();
    }

    @Override
    protected int getContentLayoutId() {
        return R.layout.fragment_mine;
    }

    @Override
    protected void initView(View rootView) {
        super.initView(rootView);
      //  EventBus.getDefault().register(this);
        hideTitle();
        llMsg.setVisibility(View.GONE);
        RetrofitHelper.getInstance().getApi()
                .getUserInfo(L.sign(L.sign(new HashMap())))
                .compose(RxHelper.<UserSelfVo>defaultHandleResult())
                .subscribe(new BaseRxSubscriber<UserSelfVo>() {
                    @Override
                    protected void onSucc(UserSelfVo userSelfVo) {


                        userInfo=userSelfVo;

                        //TODO 退出登录的时候记得清除用户信息
                        Hawk.put(L.userInfo,userSelfVo);
                        if (!EmptyUtils.isEmpty(userSelfVo.userLogo)) L.setImgHead(userSelfVo.userLogo, ivhead);
                        tvUserName.setText(userSelfVo.nikeName);
                        tvTag1.setText("id：" + userSelfVo.userId);
                        tvTag2.setText((userSelfVo.sex == 1 ? "男" : userSelfVo.sex == 2 ? "女" : "保密") + ":" + userSelfVo.province + " " + userSelfVo.city);
                        tvNum.setText("关注" + userSelfVo.focusNum + "｜粉丝" + userSelfVo.fansNum);
                        tvIntro.setText(userSelfVo.intro);


                        if(userSelfVo.dynamicPicList.length > 0){
                            myPicListRV.setLayoutManager(new LinearLayoutManager(getContext(),LinearLayoutManager.HORIZONTAL,false));
                            myPicListRV.setAdapter(new BaseQuickAdapter<String, BaseViewHolder>(R.layout.item_image_mine_list, Arrays.asList(userSelfVo.dynamicPicList.clone())) {
                                @Override
                                protected void convert(BaseViewHolder baseViewHolder, String imgUrl) {
                                    AppCompatImageView image = baseViewHolder.getView(R.id.mine_fragment_pic_item);
                                    Glide.with(getActivity()).load(ClientConfig.IMG_URL + imgUrl).into(image).
                                            onLoadStarted(getContext().getDrawable(R.drawable.image_loading));
                                }
                            });

                        }

                    }
                })
        ;
        RetrofitHelper.getInstance().getApi()
                .fileGetBaseUrl(L.sign(new HashMap() {{
                }}))
                .compose(RxHelper.<BaseString>defaultHandleResult())
                .subscribe(new BaseRxSubscriber<BaseString>() {
                    @Override
                    protected void onSucc(BaseString o) {
                        ClientConfig.IMG_URL = o.string;
                    }
                })
        ;

    }


    @OnClick({R.id.llMyNews, R.id.myShopLayout, R.id.myOrderLayout, R.id.myIdleLayout, R.id.shoppingCartLayout, R.id.walletLayout, R.id.myFavoritesLayout, R.id.setLayout})
    public void onViewClicked(View view) {
     //   if (EmptyUtils.isEmpty(userInfo)) return;
        switch (view.getId()) {
            case R.id.myOrderLayout:
                addFragment(new OrderListFragment());
                break;
            case R.id.llMyNews:
                addFragment(BlankFragment.newInstance(1));
                break;
            case R.id.myShopLayout:
                if (userInfo.shopApply == 1) {
                    L.showToast("你已提交资料审核中，请耐心等待");
                    return;
                }
                if (userInfo.shopApply == 2) {
                    addFragment(ShopIndex2Fragment.newInstance());
                    return;
                }
                if (userInfo.shopApply == 3) {
                    L.showToast("你的提交已被驳回，请重新提交审核");
                }
                addFragment(ShopApplyFragment.newInstance());
                break;
            case R.id.myFavoritesLayout:
                addFragment(MyCollectionFragment.newInstance());
                break;
            case R.id.walletLayout:
                addFragment(WalletFragment.newInstance());
                break;
            case R.id.setLayout:
                addFragment(SettingFragment_mm.newInstance());
                break;
            case R.id.editInfoIv:
                addFragment(EditInfoFragment.newInstance());
                break;
            case R.id.shoppingCartLayout:
                 addFragment(new ShoppingCardFragment());
                 break;
            case R.id.myIdleLayout:
                addFragment(new MyFreeTimeFragment());
                break;


        }
    }

}

